<template>
  <wbc-message :comments="commentData" @reply="replyComment" @add="addComment" @likeClick="likeClick"></wbc-message>
</template>

<script>
  import comments from '../components/comment'
  import {addComment,replyComment,getAllCommentByArticleId} from '../api/comment'
  import {addFavourite,removeCommentLike} from "../api/favourite";

  export default {
        name: "terComment",
      data() { //选项 / 数据
        return {
          //留言列表
          commentData:[
          ],
          //一级评论
          firstComment:{
            userId:this.$store.state.userId,
            articleId:this.$store.state.articleId,
            content:'',
            likeNum:0
          },
          //二级评论，即评论的回复
          secondComment:{
            commentId:-1,
            userId:this.$store.state.userId,
            toId:-1,
            content:'',
            articleId:this.$store.state.articleId
          }
        }
      },

      components: { //定义组件
        'wbc-message':comments
      },
      created() { //生命周期函数
        //拉去留言信息
        this.initComment()
      },
      methods:{
        addComment:function (comment) {
          let that = this
          this.firstComment.content = comment
          this.firstComment.userId = this.$store.state.userId
          this.firstComment.articleId = this.$store.state.articleId
          addComment(this.firstComment).then(response =>{
            that.initComment()
            that.$message("我收到啦~！")
          })
        },
        /**
         * @param commentId  评论id
         * @param content    回复内容
         */
        replyComment:function (content,comment,reply) {
          let that = this
          this.secondComment.commentId = comment.id
          this.secondComment.content = content
          if( typeof(reply) !== "undefined"){ //判断是否有特定的回复对象
            this.secondComment.toId = reply.fromId
          }
          replyComment(this.secondComment).then(response =>{
            that.initComment()
            that.clearSecondComment()
            that.$message("我收到啦~！")
          })
        },
        clearSecondComment(){
          this.secondComment.userId = this.$store.state.userId
          this.secondComment.articleId = this.$store.state.articleId
          this.secondComment.toId = -1
          this.secondComment.commentId = -1
          this.secondComment.content = ''
        },
        likeClick:function (item) {
          if(item.likeFlag){
            addFavourite(this.$store.state.userId,this.$store.state.username,null,
              2,1,item.id).then(response =>{

            })
          }else{
              removeCommentLike(item.id,this.$store.state.username).then(response =>{

              })
          }
        },

        initComment:function(){
          let that = this
          getAllCommentByArticleId(this.$store.state.articleId,this.$store.state.userId).then(response =>{
            that.commentData = response.data
          })
        }
      }
    }
</script>

<style scoped>

</style>
